public with sharing class AccountSalerRightController {

	public String msg {get; private set;} 
	public Boolean isErr {get; private set;} 
	public Boolean isSuccess {get ; private set;}
    public class ControllerException extends Exception {}
    public String dataJson {get;private set;}
    public String saveJson {get;set;}
    public String backId {get;private set;}
    public AccountSalerRightController() {
        backId = System.currentPageReference().getParameters().get('id');

    }

    public void init(){

    	try {
    		if(backId == null)setError('页面打开错误!');
    		if(Id.valueOf(backId).getSobjecttype() == RightsConfirmation__c.Sobjecttype ){

    			Map<String,Object> dataMap = new Map<String,Object>();
    			dataMap.put('detail',getDetails()); 

    			dataMap.put('RightsConfirmationList__c',getSODField()); 
				dataMap.put('combobox',getCombox('RightsConfirmationList__c',new Set<String>{'RightsType__c','Permissions__c','PruductLine__c'}));
    			dataJson = EncodingUtil.urlEncode(JSON.serialize(dataMap),'UTF-8');

    		}else{
    			setError('页面打开错误!');
    		}
    	    
    	}catch (Exception e) {
    	   ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
  
    	}
    	
    }

    public void save(){
    	msg = '';
    	List<Object> resList=(List<Object>)JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
    	List<RightsConfirmationList__c> rList = new List<RightsConfirmationList__c>();
    	for(Object o : resList){
    		RightsConfirmationList__c rl = new RightsConfirmationList__c();
    		Map<String,Object> mo = (Map<String,Object>) o ; 
    		rl.Id = DataUtil.getStringValue(mo.get('id'));
    		//rl.RightsConfirmation__c = DataUtil.getStringValue(mo.get('hid'));
    		rl.User__c = DataUtil.getStringValue(mo.get('salerId'));
    		rl.Account__c = DataUtil.getStringValue(mo.get('accId'));
    		rl.Permissions__c = DataUtil.getStringValue(mo.get('permission'));
    		rl.RightsType__c = DataUtil.getStringValue(mo.get('type'));
    		rl.PruductLine__c = DataUtil.getStringValue(mo.get('productLine'));
    		rl.Reason__c = DataUtil.getStringValue(mo.get('reason'));
    		rl.ReviceUser__c = DataUtil.getStringValue(mo.get('recieveId'));
    		rList.add(rl);

    	}
    	SavePoint sp = Database.setSavepoint();
    	try {
    	    
    	    update rList;
    	    isSuccess = true;
    	    msg = '保存成功!';
    	}
    	catch (Exception e) {
    		Database.rollback(sp);
    		msg = '保存失败:'+e.getMessage();
    	    
    	}
    	


    }

    private Map<String ,Map<String,String>> getCombox(String objName ,Set<String> fields ){
    	Map<String ,Map<String,String>> tmp = new Map<String ,Map<String,String>>();
    	for(String f : fields){
    		Map<String,String> pickMap = ImportObject.getPicklistValues(objName, f, true);
    		tmp.put(f, pickMap);
    	}
    	return tmp;
    } 

     private Map<String,List<DataUtil.FieldBean>> getSODField(){
    	Map<String,List<DataUtil.FieldBean>> searchMap = new Map<String,List<DataUtil.FieldBean>>();

    	Map<String,DataUtil.FieldBean>proMap = DataUtil.getFieldsInfo(RightsConfirmationList__c.SObjecttype, new Set<String>{'Permissions__c','PruductLine__c'} );
    	searchMap.put('RightsConfirmationList__c', proMap.values());
    	return searchMap;
    }
    private List<ASBean> getDetails(){
    	List<ASBean> tmp = new List<ASBean>();
    	for(RightsConfirmationList__c rc :[select Id,Reason__c,Account__c,Account__r.Name , 
    		RightsType__c,Permissions__c,User__c,User__r.Name ,ReviceUser__c,
    		ReviceUser__r.Name,User__r.FXProductLine__c,RightsConfirmation__c
    		from RightsConfirmationList__c where RightsConfirmation__c =:backId  Order by Account__c ]){
    		tmp.add( new ASBean(rc));
    	}
    	return tmp;

    }

    //设置错误
    private void setError(String message){
    	isErr = true;
        throw new ControllerException(message);

    }

    public void setNil(){
    	dataJson = '{}';
    	saveJson = '[]';
    }

    public class ASBean{

    	public String id ;//记录di 
    	public String hid ; //主表id 
    	public String accId ; //客户id
    	public String accName; //客户名称
    	public String salerId ;//现在销售员

    	public String salerName ; //现在销售员

    	public String type ; //权限类型

    	public String permission;//权限是否保留

    	public String productLine ; //现销售员负责的产品线

    	public String recieveId ; //接手销售员

    	public String recieveName ; //接手销售员
    	public String reason ;


    	public ASBean(RightsConfirmationList__c rc ){

    		id = rc.Id;
    		hid = rc.RightsConfirmation__c;
    		accId = rc.Account__c;
    		accName = rc.Account__r.Name ;
    		salerId = rc.User__c;
    		salerName = rc.User__r.Name ;

    		type = rc.RightsType__c;
    		permission = rc.Permissions__c;

    		productLine  = rc.User__r.FXProductLine__c;

    		recieveId = rc.ReviceUser__c;
    		recieveName = rc.ReviceUser__r.Name;
    		reason = rc.Reason__c;
    	}



    }



}